package com.atguigu.spzx.user.service.impl;

import com.atguigu.spzx.common.constant.Constant;
import com.atguigu.spzx.user.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public void sendValidateCode(String phone) {
        //判断这个手机号是否最近发过验证码
        String code = redisTemplate.opsForValue().get("phone:code:" + phone);
        if (StringUtils.hasText(code)){
            return;
        }

        //生成一个四位验证码
        Random random=new Random();
        String validateCode = String.valueOf(random.nextInt(9000)+1000);
        //TODO 发验证码
        sendSms();
        System.out.println("验证码是："+validateCode);
        //保存到redis
        //TODO 改时间
        redisTemplate.opsForValue().set(Constant.REDIS_PHONE_CODE + phone ,validateCode,5000, TimeUnit.MINUTES);
    }

    private void sendSms() {

    }
}
